package org.hackystat.iw.projectoverviewer;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Properties;
import java.util.logging.Logger;

import org.apache.wicket.Page;
import org.apache.wicket.protocol.http.WebApplication;
import org.hackystat.iw.projectoverviewer.jaxb.projects.Project;
import org.hackystat.iw.projectoverviewer.jaxb.projects.Projects;
import org.hackystat.iw.projectoverviewer.xml.XmlConverter;
import org.hackystat.iw.projectoverviewer.page.newproject.NewProjectPage;
import org.hackystat.iw.projectoverviewer.page.projects.ProjectsPage;
import org.hackystat.utilities.logger.HackystatLogger;

/**
 * The top-level web application instance for this ProjectOverviewer.
 * 
 * @author Philip Johnson
 * @author Jianfei Liao
 */
public class ProjectOverviewerApplication extends WebApplication {
  
  /** 
   * Project properties are read from ~/.hackystat/projectoverviewer/projectoverviewer.properties, 
   * however some of those values (i.e. the locations of the hackystat services) can be 
   * overridden for testing purposes. 
   */
  private ProjectOverviewerProperties properties;
  
  /** Holds the HackystatLogger for this Service. */
  private Logger logger;
  
  /** Holds information for all projects in this application. */
  private Projects projects;
  
  /**
   * Creates a ProjectOverviewerApplication, obtaining all ProjectOverviewerProperties 
   * from the properties file.
   */
  public ProjectOverviewerApplication() {
    this(null);
  }
  
  /**
   * Creates a ProjectOverviewerApplication, in which the passed properties override the entries
   * in the properties file.  
   * @param properties A properties instance, the contents of which will override any other 
   * property settings. 
   */
  public ProjectOverviewerApplication(Properties properties) {
    this.logger = HackystatLogger.
        getLogger("org.hackystat.iw.projectoverviewer", "projectoverviewer");
    this.properties = new ProjectOverviewerProperties(properties);
    this.logger.info(this.properties.echoProperties());
  }
  

  /**
   * Returns the home page for this application (ProjectsPage).
   * @return The home page. 
   */
  @Override
  public Class<? extends Page> getHomePage() {
    return ProjectsPage.class;
  }
  
  /**
   * Do default setup and initialization when this web application is started up. 
   */
  @Override
  public void init() {
    // locate the projects.xml file and read the projects info from it
    String userHome = System.getProperty("user.home");
    File file = new File(userHome + "/.hackystat/projectoverviewer/projects.xml");
    if (file.exists()) {
      String packageName = "org.hackystat.iw.projectoverviewer.jaxb.projects";
      projects = (Projects)XmlConverter.xmlToObject(file, packageName);
    }
    else {
      try {
        file.createNewFile();
      }
      catch (IOException e) {
        e.printStackTrace();
      }
      projects = new Projects();
    }
    
    mountBookmarkablePage("new-project", NewProjectPage.class);
    super.init();
  }
  
  /**
   * Returns the ProjectOverviewerProperties instance associated with this web app.
   * @return The properties.
   */
  public ProjectOverviewerProperties getProjectOverviewerProperties() {
    return this.properties;
  }
  
  /**
   * Returns the value associated with key, or null if not found. Key should be one of the public
   * static strings declared in ProjectOverviewerProperties. 
   * @param key The key. 
   * @return The value associated with this key, or null if not found. 
   */
  public String getProjectOverviewerProperty(String key) {
    return this.properties.get(key);
  }
  
  /**
   * Returns the logger for this service.
   * @return The logger.
   */
  public Logger getLogger() {
    return this.logger;
  }
  
  /**
   * Returns all the projects in this application.
   * @return The projects instance.
   */
  public Projects getProjects() {
    return this.projects;
  }
  
  /**
   * Removes a project.
   * 
   * @param projectName The name of the project to be removed.
   * @return true if the project is removed, false otherwise.
   */
  public boolean removeProject(String projectName) {
    boolean isRemoved = true;
    
    int index = -1;
    List<Project> projectsList = this.projects.getProject();
    // find the matching project to delete
    for (Project project : projectsList) {
      if (projectName.equals(project.getProjectName())) {
        index = projectsList.indexOf(project);
      }
    }
    // try to delete the project
    try {
      this.projects.getProject().remove(index);
    }
    catch (Exception e) {
      isRemoved = false;
    }
    
    return isRemoved;
  }

  /**
   * Returns the sensorbase host.
   * @return The sensorbase host. 
   */
  public String getSensorBaseHost() {
    return getProjectOverviewerProperty(ProjectOverviewerProperties.SENSORBASE_HOST_KEY);
  }
  
  /**
   * Returns the application name.
   * @return The application name.
   */
  public String getApplicationName() {
    return getProjectOverviewerProperty(ProjectOverviewerProperties.APPLICATION_NAME_KEY);
  }
  

}
